package com.bw.z.mvp.view.live;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bw.z.R;
import com.bw.z.mvp.HttpCode;
import com.bw.z.mvp.base.view.BaseFragment;
import com.bw.z.mvp.contract.NewsContract;
import com.bw.z.mvp.di.DaggerNewsComponent;
import com.bw.z.mvp.di.NewsModules;
import com.bw.z.mvp.model.entity.BaseEntity;
import com.bw.z.mvp.model.entity.LiveEntity;
import com.bw.z.mvp.model.entity.TextBannerEntity;
import com.bw.z.mvp.presenter.NewsPresenter;
import com.bw.z.mvp.view.live.adapter.LiveAdapter;
import com.bw.z.mvp.view.news.adapter.TextBannerAdapter;
import com.bw.z.utils.ToastUtils;
import com.bw.z.widget.LiveNewsItemDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import com.youth.banner.transformer.ZoomOutPageTransformer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LiveFragment extends BaseFragment<NewsPresenter> implements NewsContract.NewView, OnRefreshLoadMoreListener, OnItemClickListener {

    private SmartRefreshLayout liveSmart;
    private RecyclerView liveRv;
    private View liveHead, liveHot, liveTitle;
    private Banner<TextBannerEntity.Values, TextBannerAdapter> live_head_banner;
    private LiveAdapter liveAdapter;
    private TextBannerAdapter textBannerAdapter;
    private List<LiveEntity.Values> list = new ArrayList<>();

    @Override
    public int bindLayout() {
        return R.layout.fragment_live;
    }

    @Override
    public void initView() {
        liveSmart = f(R.id.live_smart);
        liveRv = f(R.id.live_rv);
        liveHead = LayoutInflater.from(getContext()).inflate(R.layout.view_live_head, null);
        liveHot = LayoutInflater.from(getContext()).inflate(R.layout.view_live_hot, null);
        liveTitle = LayoutInflater.from(getContext()).inflate(R.layout.view_live_title, null);
        live_head_banner = liveHead.findViewById(R.id.live_head_banner);
        liveSmart.setEnableLoadMore(true);
        liveSmart.setEnableRefresh(true);
        liveSmart.setRefreshHeader(new ClassicsHeader(getContext()));
        liveSmart.setRefreshFooter(new ClassicsFooter(getContext()));
        liveSmart.setOnRefreshLoadMoreListener(this);
        GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        liveRv.setLayoutManager(manager);
        liveRv.addItemDecoration(new LiveNewsItemDecoration(getContext()));
        liveAdapter = new LiveAdapter();
        liveAdapter.addHeaderView(liveHead);
        liveAdapter.addHeaderView(liveHot);
        liveAdapter.addHeaderView(liveTitle);
        liveRv.setAdapter(liveAdapter);

        liveAdapter.setOnItemClickListener(this::onItemClick);
    }

    @Override
    public void inject() {
        DaggerNewsComponent.builder().newsModules(new NewsModules(this)).build().injectLiveNews(this);
    }

    @Override
    public void initData() {
        Map<String, Object> bannerMap = new HashMap<>();
        bannerMap.put("code", HttpCode.TEXTBANNERCODE);
        Map<String, Object> liveMap = new HashMap<>();
        liveMap.put("code", HttpCode.LIVECODE);
        liveMap.put("user_id", 1);
        p.requestAll(bannerMap, liveMap);
    }

    @Override
    public void onStart() {
        super.onStart();
        live_head_banner.start();
    }

    @Override
    public void onStop() {
        super.onStop();
        live_head_banner.stop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        live_head_banner.destroy();
    }

    @Override
    public void refreshAll(BaseEntity baseEntity) {
        if (baseEntity instanceof TextBannerEntity) {
            TextBannerEntity textBannerEntity = (TextBannerEntity) baseEntity;
            textBannerAdapter = new TextBannerAdapter(textBannerEntity.getValues());
            live_head_banner.setAdapter(textBannerAdapter)
                    .setOrientation(Banner.VERTICAL)
                    .setPageTransformer(new ZoomOutPageTransformer());
        } else if (baseEntity instanceof LiveEntity) {
            LiveEntity liveEntity = (LiveEntity) baseEntity;
            liveAdapter.setNewInstance(liveEntity.getValues());
        }
    }

    @Override
    public void refreshRecyclerView(BaseEntity baseEntity) {
        LiveEntity liveEntity = (LiveEntity) baseEntity;
        if (liveEntity.getValues() == null) {
            return;
        }
        liveAdapter.setNewInstance(liveEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity baseEntity) {
        LiveEntity liveEntity = (LiveEntity) baseEntity;
        if (liveEntity.getValues() == null) {
            ToastUtils.showMsg("最后一页");
            return;
        }
        list = liveAdapter.getData();
        list.addAll(liveEntity.getValues());
        liveAdapter.setNewInstance(list);
        liveAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        Map<String, Object> liveMap = new HashMap<>();
        liveMap.put("code", HttpCode.LIVECODE);
        liveMap.put("user_id", 1);
        p.requestLoad(liveMap);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        if (list != null)
            list.clear();
        Map<String, Object> liveMap = new HashMap<>();
        liveMap.put("code", HttpCode.LIVECODE);
        liveMap.put("user_id", 1);
        p.requestRefresh(liveMap);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        LiveEntity.Values entity = (LiveEntity.Values) adapter.getItem(position);
        Bundle bundle = new Bundle();
        bundle.putLong("live_id", entity.live_id);
        bundle.putString("live_url", entity.live_url);
        startPage(bundle, LiveValuesActivity.class);
    }
}